<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>水费户号未绑定</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../../plugins/mui/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../../css/life_housekeeper/wate_index.css" />
		<script src="../../js/statistics.js"></script>
		<script src="../../js/fontset.js"></script>
	</head>
	<body>
		<header class="mui-bar mui-bar-nav header">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
		    <h1 class="mui-title">水费查询</h1>
		</header>
		<div class="mui-content content-vh">
			<div class="content-default">
				
				<div class="group-bg-box default-bg">
					<div class="user-img-box flex-center">
						<div class="user-img-box-line flex-center">
							<span class="user-img"><img src="" id="userImg"/></span>
						</div>
					</div>
					<div class="search-group group-top">
						<div class="group-tit mui-text-center">
							<h3 id="userName"></h3>
							<p>您当前尚未绑定户号</p>
						</div>
						<div class="search-view-cell">
							<label for="user" class="search-label">户号&nbsp;:</label>
							<input type="number" placeholder="请输入用户号" class="search-input input-defalut" id="userCode"/>
						</div>	
					</div>
				</div>
			    
				<div class="btn-box">
					<button class="button-primary" id="bindBtn">绑定</button>
				</div>
				
			</div>
			<div class="footer-img-defalut">
				<img src="../../images/icon/life_housekeeper/user-water-bg2.png" class="img-response"/>
			</div>
		</div>
		<script src="../../plugins/mui/mui.min.js"></script>
		<script src="../../js/getApiToken.js"></script>
		<script src="../../js/md5.js"></script>
		<script src="../../plugins/template/template.js"></script>
		<script src="../../js/life_housekeeper/windowTitleView.js"></script>
		<script type="text/javascript">
			mui.init();
			mui.plusReady(function(){
				var headimgUrl = plus.storage.getItem("cg_headimgurl"),
					userImg = document.getElementById("userImg");
				userImg.src = headimgUrl;
				
				var waterCode = plus.storage.getItem('water_code');
				if(!waterCode || waterCode == ''){
					certification(waterBind);
				}else{
					var str = '请绑定新户号';
					document.querySelectorAll(".search-group p")[0].innerHTML = str;
					waterBind(waterCode);
				}
			})
			//户号绑定
			function waterBind(e){
				var nickName = plus.storage.getItem("cg_nickname"),
				    userId = plus.storage.getItem("cg_user_id"),
				    userName = document.getElementById("userName"),
				    userCode = document.getElementById("userCode"),
					bindBtn  = document.getElementById("bindBtn");
				userName.innerHTML = nickName;
				bindBtn.addEventListener('tap',function(){					
			    	var value = userCode.value;
			    	console.log(value);
			    	if(!value || value==""){
			    		mui.toast("请输入户号!");
			    		return;
			    	}else{
			    		var reg = new RegExp("^[0-9]*$");
			    		if(!reg.test(value)){
					        mui.toast("请输入数字!");
					        return;
					    }else{
					    	var data ={
					    		code:value,
					    		userId:userId
					    	};
					    	if(e){
					    		var arr = e.split(','),status;
						    	for(var b in arr){
						    		if(value == arr[b]){
						    			status = true;
						    		}
						    	}
						    	if(status){
						    		mui.toast("该户号已绑定，勿重复绑定相同户号!");
						    		return;
						    	}else{
						    		data.code = e +','+ value;
						    	}
						    	console.log(data.code);
					    	}
					    	plus.nativeUI.showWaiting();
					    	myAjax('home/water/bind','get',data,function(result){
					    		plus.nativeUI.closeWaiting();
					    		var state = result.code;
					    		if(state == 0){
									var viewOptions = {
										url:'user_water_list.html',
										id:'user_water_list'
									},
									extras ={code:data.code},
									titleOptions = {
										title:'水费查询',
										backImg:'whiteBack'
									};
									//水务户号赋值
									plus.storage.setItem('water_code',data.code);
									var queryPage = plus.webview.getWebviewById('user_water_list');
									
									if(queryPage){
										mui.fire(queryPage,'bindEvent',extras);
									}
									addTitleView(viewOptions,extras,titleOptions);
					    		}
					    	})
					    }
					}
					
				},false)
			}
			//实名认证检验
			function certification(callback){
				plus.nativeUI.showWaiting();
				//实名认证 每次都需要实时获取 除了已经认证成功
				if(plus.storage.getItem('cg_certification')!=1){
					myAjax('personal/user_profile/getselfprofile', 'get', {
						'user_id': plus.storage.getItem('cg_user_id'),
						'user_token': plus.storage.getItem('cg_user_token')
					}, function(data) {
						plus.nativeUI.closeWaiting();
						if(!data.success){
							plus.nativeUI.alert(data.error_msg, function() {
								mui.back();
							}, "错误", "确定");
							return ;//发生错误 就直接退出
						}
						plus.storage.setItem('cg_certification', data.certification);
						var certification = plus.storage.getItem('cg_certification') ;
						var	confirmArray = ['前往认证','稍后认证'];
						if(certification==-1){
							mui.confirm('您尚未进行实名认证，无法进行水费查询。','实名认证',confirmArray,function(e){
								if(e.index == 0){
									var self = plus.webview.currentWebview();
									self.close();
									mui.openWindow({
									    url:'../my/user-4-1.html',
									    id:'user-4-1.html',
									     show:{
										      autoShow:false,//页面loaded事件发生后自动显示，默认为true
										      duration:200//页面动画持续时间，Android平台默认100毫秒，iOS平台默认200毫秒；
										},
										 waiting:{
									      	title:'正在加载...'//等待对话框上显示的提示内容
									     }
									});	
									return;
								}
								mui.back();
							})
							return;
						}else{
							if(certification == 0){
								mui.alert('您的实名认证正在审核中，请耐心等待。','实名认证','确定',function(e){									
									mui.back();
								})
							}else if(certification == 2){
								mui.confirm('您的实名认证未通过，请重新认证。','实名认证',confirmArray,function(e){
									if(e.index == 0){
										var self = plus.webview.currentWebview();
										self.close();
										mui.openWindow({
										    url:'../my/user-4.html',
										    id:'user-4.html',
										     show:{
											      autoShow:false,//页面loaded事件发生后自动显示，默认为true
											      duration:200//页面动画持续时间，Android平台默认100毫秒，iOS平台默认200毫秒；
											},
											 waiting:{
										      	title:'正在加载...'//等待对话框上显示的提示内容
										     }
										});	
										return;
									}
									mui.back();
								})
							}else if(certification == 1){
								callback();
							}
						}
					});
				}else{
					plus.nativeUI.closeWaiting();
					callback();
				}
			}
		</script>
	</body>
</html>